#!/usr/bin/env bash
# vim: fdm=marker fmr=[[[,]]] cms=\ #%s

bin_path=$(dirname $0)
if [ ${bin_path:0:1} != '/' ]
then
    bin_path=$(pwd)/$bin_path
fi
CLIENT=$bin_path/../../tool/snclient
ADDR=localhost:10030

GETTS()
{ #[[[
    $CLIENT $ADDR GETTS
} #]]]

DIFFTS()
{ #[[[
    local ts
    read -p "diff_ts: " ts
    $CLIENT $ADDR DIFFTS $ts
} #]]]

ENCRYPT()
{ #[[[
    local pubkey
    read -p "pubkey[0000]: " pubkey
    local plain_text
    read -p "plain_text[Hello world!]: " plain_text
    $CLIENT $ADDR ENCRYPT "${pubkey:-0000}" "${plain_text:-Hello world!}"
} #]]]

DECRYPT()
{ #[[[
    local encrypt_text
    read -p "encrypt_text: " encrypt_text
    $CLIENT $ADDR DECRYPT "${encrypt_text}"
} #]]]

SIGN()
{ #[[[
    local plain_text
    read -p "plain_text[Hello world!]: " plain_text
    $CLIENT $ADDR SIGN "${plain_text:-Hello world!}"
} #]]]

VERIFY()
{ #[[[
    local pubkey
    read -p "pubkey[0000]: " pubkey
    local plain_text
    read -p "plain_text[Hello world!]: " plain_text
    local sign_text
    read -p "sign_text: " sign_text
    $CLIENT $ADDR VERIFY "${pubkey:-0000}" "${plain_text:-Hello world!}" "${sign_text}"
} #]]]

ENCSIGN()
{ #[[[
    local pubkey
    read -p "pubkey[0000]: " pubkey
    local plain_text
    read -p "plain_text[Hello world!]: " plain_text
    $CLIENT $ADDR ENCSIGN "${pubkey:-0000}" "${plain_text:-Hello world!}"
} #]]]

VRFYDEC()
{ #[[[
    local pubkey
    read -p "pubkey[0000]: " pubkey
    local encrypt_text
    read -p "encrypt_text: " encrypt_text
    local sign_text
    read -p "sign_text: " sign_text
    $CLIENT $ADDR VRFYDEC "${pubkey:-0000}" "${encrypt_text}" "${sign_text}"
} #]]]

MAKEUSERPASSWD()
{ #[[[
    local passwd
    read -p "passwd[11111111]: " passwd
    $CLIENT $ADDR MAKEUSERPASSWD "${passwd:-11111111}"
} #]]]

VERIFYUSERPASSWD()
{ #[[[
    local passwd
    read -p "passwd[11111111]: " passwd
    local enc_passwd
    read -p "encrypted-passwd: " enc_passwd
    $CLIENT $ADDR VERIFYUSERPASSWD "${passwd:-11111111}" "$enc_passwd"
} #]]]

DECUSERPASSWD()
{ #[[[
    local passwd_enc
    read -p "passwd_enc: " passwd_enc
    $CLIENT $ADDR DECUSERPASSWD "${passwd_enc}"
} #]]]

ENCUSERMSG()
{ #[[[
    local plain
    read -p "plain[hello world!]: " plain
    $CLIENT $ADDR ENCUSERMSG "${plain:-hello world!}"
} #]]]

DECUSERMSG()
{ #[[[
    local code
    read -p "code: " code
    $CLIENT $ADDR DECUSERMSG "${code}"
} #]]]

main()
{ #[[[
    if [[ ! -x $CLIENT ]]
    then
        echo "snclient not found"
        return 1
    fi

    local option
    read -p "\
Services:
    1) GETTS
    2) DIFFTS ts
    3) ENCRYPT pubkey plain_text
    4) DECRYPT encrypt_text
    5) SIGN plain_text
    6) VERIFY pubkey plain_text sign_text
    7) ENCSIGN pubkey plain_text
    8) VRFYDEC pubkey encrypt_text sign_text

    9) MAKEUSERPASSWD passwd
    a) VERIFYUSERPASSWD passwd encrypted-passwd
    b) DECUSERPASSWD passwd
    c) ENCUSERMSG plain
    d) DECUSERMSG code
Input: " option
    if [[ -z $option ]]
    then
        echo  "Error: option invalid"
        return 1
    fi
    case $option in
    1) GETTS ;;
    2) DIFFTS ;;
    3) ENCRYPT ;;
    4) DECRYPT ;;
    5) SIGN ;;
    6) VERIFY ;;
    7) ENCSIGN ;;
    8) VRFYDEC ;;
    9) MAKEUSERPASSWD ;;
    a) VERIFYUSERPASSWD ;;
    b) DECUSERPASSWD ;;
    c) ENCUSERMSG ;;
    d) DECUSERMSG ;;
    *)
        echo "Error: option($option) not supported"
        return 1
        ;;
    esac
} #]]]

main
exit $?
